/*
 *  This file is part of jMoviepilot.
 *  Copyright (C) 2011 Tobias Roettger <toroettg@gmail.com>
 *
 *  jMoviepilot is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *  
 *  The project site can be found at http://code.google.com/p/jmoviepilot/
 */

package de.roettger_it.jmoviepilot.util;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * This class contains information about the poster that is related to a movie.
 * 
 * @author Tobias Roettger
 * 
 */
public class Poster
{	
	public enum Format { RAW, NORMAL , THUMB_SQUARE , POSTER, POSTER_LARGE, PERSON, ARTICLE };
	
	private String type;
	
	private String file_name_base;
	
	private String mime_type;
	
	private String copyright;
	
	private String extension;
	
	private String title;
	
	private URL base_url;
	private URL restful_url;
	
	private Integer height;
	private Integer width;
	private Integer size;
	
	private String photo_id;
	
	/**
	 * Gets the type of the poster.
	 * 
	 * @return the type of the poster
	 */
	public String getType()
	{
		return type;
	}

	/**
	 * Sets the type of the poster.
	 * 
	 * @param type - the type to set
	 */
	public void setType(String type)
	{
		this.type = type;
	}

	/**
	 * Gets the filename base of the poster (part of the poster's URL).
	 * 
	 * @return the filename base of the poster
	 */
	public String getFileNameBase()
	{
		return file_name_base;
	}

	/**
	 * Sets the filename base of the poster (part of the poster's URL).
	 * 
	 * @param fileNameBase the faleNameBase to set
	 */
	public void setFileNameBase(String fileNameBase)
	{
		this.file_name_base = fileNameBase;
	}

	/**
	 * Gets the MIME type of the poster.
	 * 
	 * @return the MIME type of the poster
	 */
	public String getMimeType()
	{
		return mime_type;
	}

	/**
	 * Sets the MIME type of the poster.
	 * 
	 * @param mimeType the MIME type to set
	 */
	public void setMimeType(String mimeType)
	{
		this.mime_type = mimeType;
	}

	/**
	 * Gets the copyright owner of the poster.
	 * 
	 * @return the copyright of the poster
	 */
	public String getCopyright()
	{
		return copyright;
	}

	/**
	 * Sets the copyright owner of the poster.
	 * 
	 * @param copyright the copyright to set
	 */
	public void setCopyright(String copyright)
	{
		this.copyright = copyright;
	}

	/**
	 * Gets the file extension of the poster (part of the poster's URL).
	 * 
	 * @return the extension of the poster
	 */
	public String getExtension()
	{
		return extension;
	}

	/**
	 * Sets the file extension of the poster (part of the poster's URL).
	 * 
	 * @param extension the extension to set
	 */
	public void setExtension(String extension)
	{
		this.extension = extension;
	}

	/**
	 * Gets the title of the poster.
	 * 
	 * @return the title of the poster
	 */
	public String getTitle()
	{
		return title;
	}

	/**
	 * Sets the title of the poster.
	 * 
	 * @param title the title to set
	 */
	public void setTitle(String title)
	{
		this.title = title;
	}

	/**
	 * Gets the base URL of the poster (part of the poster's URL).
	 * 
	 * @return the base URL of the poster
	 */
	public URL getBaseURL()
	{
		return base_url;
	}

	/**
	 * Sets the base URL of the poster (part of the poster's URL).
	 * 
	 * @param baseURL the base URL to set 
	 */
	public void setBaseURL(URL baseURL)
	{
		this.base_url = baseURL;
	}

	/**
	 * Gets the restful URL of the poster.
	 * 
	 * @return the restful URL of the poster
	 */
	public URL getRestfulURL()
	{
		return restful_url;
	}

	/**
	 * Sets the restful URL of the poster.
	 * 
	 * @param restfulURL the restful URL to set
	 */
	public void setRestfulURL(URL restfulURL)
	{
		this.restful_url = restfulURL;
	}

	/**
	 * Gets the height of the poster.
	 * 
	 * @return the height of the poster in pixel
	 */
	public Integer getHeight()
	{
		return height;
	}

	/**
	 * Sets the height of the poster.
	 * 
	 * @param height the height to set in pixel
	 */
	public void setHeight(int height)
	{
		this.height = height;
	}

	/**
	 * Gets the width of the poster.
	 * 
	 * @return the width of the poster in pixel
	 */
	public Integer getWidth()
	{
		return width;
	}

	/**
	 * Sets the width of the poster.
	 * 
	 * @param width the width to set in pixel
	 */
	public void setWidth(int width)
	{
		this.width = width;
	}

	/**
	 * Gets the size of the poster.
	 * 
	 * @return the size of the poster in bytes
	 */
	public Integer getSize()
	{
		return size;
	}

	/**
	 * Sets the size of the poster.
	 * 
	 * @param size the size to set in pixel
	 */
	public void setSize(int size)
	{
		this.size = size;
	}

	/**
	 * Gets the photo ID of the poster (part of the poster's URL).
	 * 
	 * @return the photo ID of the poster
	 */
	public String getPhotoID()
	{
		return photo_id;
	}

	/**
	 * Sets the photo ID of the poster (part of the poster's URL).
	 * 
	 * @param photoID the photo ID to set
	 */
	public void setPhotoID(String photoID)
	{
		this.photo_id = photoID;
	}
	
	/**
	 * Gets the URL of the poster in unmodified size. The URL is composed of the base URL,
	 * the photo ID, the filename base and the extension of the poster.
	 * 
	 * 
	 * @return the composed poster URL, or null if one of its components doesn't exist
	 */
	public URL getPosterURL()
	{
		return getPosterURL(Poster.Format.RAW);
	}
	
	/**
	 * Gets the URL of the poster in a specific size. The URL is composed of the base URL,
	 * the photo ID, the filename base and the extension of the poster.
	 * 
	 * @param format determines the size of the poster. Available formats are raw (unmodified), normal (200xY),
	 * squared thumbnail (100x100), poster (60xY), large poster (108x152), person (168xY) and article (480xY).
	 * The Y value is variable for some formats, so that the poster's aspect ratio is kept.
	 * 
	 * @return the composed poster URL, or null if one of its components doesn't exist
	 */
	public URL getPosterURL(Poster.Format format)
	{
		if(getBaseURL() == null || getPhotoID() == null || getFileNameBase() == null || getExtension() == null)
		{
			return null;
		}
		
		String formatString = format == null || format.toString().equals("RAW") ? "" : "_" + format.toString().toLowerCase();
		
		URL posterURL = null;
		
		try
		{
			posterURL = new URL(getBaseURL() + getPhotoID() + "/" + getFileNameBase() + formatString + "." + getExtension());
		}
		catch(MalformedURLException mue)
		{			
			mue.printStackTrace(); //Shouldn't happen since baseURL is already an URL instance
		}
		
		return posterURL;
	}
}
